package com.hupu.jrs.data.local

import com.hupu.jrs.data.ForumDataSource
import javax.inject.Inject
import rx.Observable
import rx.schedulers.Schedulers

/**
 * Created by sll on 2016/6/3.
 */
class ForumLocalDataSource @Inject
constructor(private val mForumDao: ForumDao) : ForumDataSource {

    override fun getForumList(forumId: String): Observable<List<Forum>> {
        return Observable.create(Observable.OnSubscribe<List<Forum>> { subscriber ->
            val forumList = mForumDao.queryBuilder().where(ForumDao.Properties.ForumId.eq(forumId)).list()
            subscriber.onNext(forumList)
            subscriber.onCompleted()
        }).subscribeOn(Schedulers.io())
    }

    fun saveForum(forum: Forum) {
        mForumDao.queryBuilder()
                .where(ForumDao.Properties.ForumId.eq(forum.forumId),
                        ForumDao.Properties.Fid.eq(forum.fid))
                .buildDelete()
                .executeDeleteWithoutDetachingEntities()
        mForumDao.insert(forum)
    }

    override fun removeForum(fid: String): Observable<Boolean>? {
        return null
    }
}
